home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / ZCATALOG.TXT < prev    next >
Encoding:
Text File  |  1999-08-16  |  17.9 KB  |  465 lines

  1. ZCatalog Tutorial
  2.  
  3.   This document provides a tutorial for 'ZCatalog', the new search
  4.   engine machinery in Zope.  The audience for the document is content
  5.   managers.
  6.  
  7.   Contents
  8.  
  9.     o What is it?  What's it for?  Why's it so cool?
  10.  
  11.     o Installing ZCatalog
  12.  
  13.     o ZCatalog Objects
  14.  
  15.     o Example using ZCatalog
  16.  
  17.     o Creating Search Forms And Result Reports
  18.  
  19.     o Using ZCatalog In A Zope Site
  20.  
  21.     o ZCatalog vs. Catalog
  22.  
  23.   What is it?  What's it for?  Why's it so cool?
  24.  
  25.     The 'ZCatalog' provides powerful indexing and searching on a Zope
  26.     database using a Zope management interface.  A 'ZCatalog' is a
  27.     Zope object that can be added to a Folder, managed through the
  28.     web, and extended in many ways.
  29.  
  30.     The 'ZCatalog' is a very significant project, providing a number
  31.     of compelling features:
  32.  
  33.       o **Searches are fast**.  The data structures used by the index
  34.       provide extremely quick searches without consuming much memory.
  35.  
  36.       o **Searches are robust**.  The 'ZCatalog' supports boolean
  37.       search terms, proximity searches, synonyms and stopwords.
  38.  
  39.       o **Indexing is wildly flexible**.  A 'ZCatalog' can catalog
  40.       custom properties and track unique values.  Since 'ZCatalog'
  41.       catalogs objects instead of file handles, you can index any
  42.       content that can have a Python object wrapped around it.  This
  43.       also lets objects participate in how they are cataloged,
  44.       e.g. de-HTML-ifying contents or extracting PDF properties.
  45.  
  46.       o **Usable outside of Zope**.  The software is broken into a
  47.       Python 'Catalog' which wrapped by a 'ZCatalog'.  The Python
  48.       'Catalog' can be used in any Python program; all it requires is
  49.       the Z object database and the indexing machinery from Zope.
  50.  
  51.       o **Transactional**.  An indexing operation is part of a Zope
  52.       transaction.  If something goes wrong after content is indexed,
  53.       the index is restored to its previous condition.  This also means
  54.       that Undo will restore an index to its previous condition.
  55.       Finally, a 'ZCatalog' can be altered privately in a Version,
  56.       meaning no one else can see the changes to the index.
  57.  
  58.       o **Cache-friendly**.  The index is internally broken into
  59.       different "buckets", with each bucket being a separate Zope
  60.       database object.  Thus, only the part of the index that is needed
  61.       is loaded into memory.  Alternatively, an un-needed part of the
  62.       index can be removed from memory.
  63.  
  64.       o **Results are lazy**.  A search that returns a tremendous
  65.       number of matches won't return a large result set.  Only the
  66.       part of the results, such as the second batch of twenty, are
  67.       returned.
  68.  
  69.     The 'ZCatalog' is a free, Open Source part of the Zope software
  70.     repository and thus is covered under the same license as Zope.  It
  71.     is being developed in conjunction with the Zope Portal Toolkit
  72.     effort.  However, the 'ZCatalog' product is managed as its own
  73.     module in CVS.
  74.  
  75.   Installing ZCatalog
  76.  
  77.     'ZCatalog' can be downloaded from the Zope download area and is
  78.     also a module in the public CVS for Zope.  Untar it while in the
  79.     root directory of your Zope installation::
  80.  
  81.       $ cd Zope-2.0.0a3-src/
  82.       $ tar xzf ../ZCatalog-x.x.tgz
  83.  
  84.     Windows users can use WinZip or a similar utility to accomplish
  85.     the same thing.
  86.  
  87.     Also, Zope 2.0.0a3 does not have the latest version of UnIndex and
  88.     UnTextIndex which fix a couple of bugs in the alpha 3 versions.
  89.     The latest CVS of the SearchIndex packages *must* be used.
  90.  
  91.     Remember, you have to restart your Zope server before you will see
  92.     'ZCatalog'.
  93.  
  94.   ZCatalog Objects
  95.  
  96.     A 'ZCatalog' performs two activities: indexing information and
  97.     performing searches.
  98.  
  99.     Most the work is done in the first step, which is getting objects
  100.     into the index.  This is done in two ways.  First, if your objects
  101.     are ZCatalog-aware they automatically update the index when they
  102.     are added, edited or directly deleted.  A ZCatalog-aware object is
  103.     one that is an instance of a 'Z Class' that informs the 'ZCatalog'
  104.     of changes.  *Directly deleted* means the object was deleted from
  105.     a Folder, not the deletion of a containing Folder.
  106.  
  107.     The second way that site contents get updated is by "finding"
  108.     information "into" the 'ZCatalog'.  An operation based on Zope's
  109.     Find view traverses Folders looking for objects matching the
  110.     criterion.  The objects are then registered with the Catalog.
  111.     Objects in the index but no longer in the site are removed from
  112.     the Catalog.
  113.  
  114.     Either way -- automatically updating or walking the Folders --
  115.     'ZCatalog' indexes the objects it finds.  The 'ZCatalog' is set up
  116.     to look for properties, each of which are added to the index.
  117.  
  118.     There are two kinds of indexes, called FieldIndex and TextIndex.
  119.     FieldIndex indexes treat data atomically.  The entire contents of a
  120.     FieldIndex-indexed property is treated as a unit.  With a
  121.     TextIndex index, it is broken into words which are indexed
  122.     individually.  A TextIndex is also known as *full-text index*.
  123.  
  124.     Note that the 'ZCatalog' doesn't track ZCatalog-unaware objects
  125.     after it has indexed them.  This means that the 'ZCatalog' must
  126.     reindex its objects occasionally when the objects have been
  127.     changed.  Out of date indexes can be prevented by inheriting from
  128.     a ZCatalog-aware class which can tell the 'ZCatalog' to reindex it
  129.     whenever a change is made.  Just such a class will be included
  130.     with the Portal toolkit.
  131.  
  132.     ZCatalogs are "searchable objects", meaning they cooperate with Z
  133.     Search Interfaces documented in Z SQL Methods.  Creating a search
  134.     form for a 'ZCatalog' is a simple matter of adding a Z Search
  135.     Interface from the management screen and filling in a form.
  136.     ZCatalogs can also be queried directly from DTML, as shown in the
  137.     example below.
  138.  
  139.   Example using Z Classes
  140.  
  141.     The first example shows how to give your Zope site a long-desired
  142.     feature: full text-searches of your content.  The example assumes
  143.     you already have a number of DTML Methods/Documents to catalog.
  144.  
  145.         o Install 'ZCatalog' as instructed above
  146.  
  147.         o In the root folder of your Zope server, add a 'ZCatalog'.
  148.  
  149.         o Type in the id 'catalog' and hit 'Add'.
  150.  
  151.     You now have a brand new 'ZCatalog' named 'catalog' in your root
  152.     folder.
  153.  
  154.         o Click on it.
  155.  
  156.     Now you are looking at the 'ZCatalog' 'Contents' view.  It says
  157.     the catalog is empty.  We'll catalog some objects in a moment, but
  158.     first we have to tell it what portions of objects we are
  159.     specifically interested in.
  160.  
  161.         o Click on 'Indexes'.
  162.  
  163.     This management view is where the attributes to be indexed are
  164.     defined.
  165.  
  166.         o In the 'Add index' field, type 'raw'.
  167.  
  168.         o Click 'Add'.
  169.  
  170.     Now that the indexes are defined, a set of objects can be selected
  171.     for cataloging.
  172.     
  173.         o Click on 'Find items to ZCatalog'.
  174.  
  175.     For this example, we are only interested in DTML Documents and
  176.     Methods.
  177.  
  178.         o Deselect 'All type'.
  179.  
  180.         o Select 'DTML Method' and 'DTML Document'.
  181.  
  182.         o Click 'Find'.
  183.  
  184.     ZCatalog will report how many items it found, and then present an
  185.     interface for excluding specific objects.
  186.     
  187.         o Click 'Catalog Items'.
  188.  
  189.     Great, now that the catalog is stocked, we can create a user
  190.     interface to it.
  191.     
  192.         o Return to the root folder's management view.
  193.  
  194.         o Add a 'Z Search Interface'.
  195.  
  196.     'ZCatalog' participates in the Zope Search architecture.  You
  197.     simply have to fill in this form, and a basic user interface will
  198.     be created.
  199.  
  200.         o Select 'catalog' in the list beside 'Select one or more searchable
  201.           objects'.
  202.         
  203.         o Beside 'Report Id', type 'report'.
  204.  
  205.         o Beside 'Search Input Id', type 'search'.
  206.  
  207.     'report' and 'search' are the Ids of two DTML Methods which will
  208.     be created in your root folder.
  209.  
  210.         o Click 'Add'.
  211.  
  212.     Congratulations, if all has gone well, you can now find references
  213.     to any word in your DTML pages.  Try it by viewing 'search'.  Type
  214.     a common word in the 'Raw' field, and you should be presented with
  215.     a list of hits.  However, none of the results returned can be
  216.     clicked on.  To fix this, go to the management view of 'report'.
  217.     'report' is called by 'search' to display the results from
  218.     'catalog'.  'report' is just a simple '<!--#in catalog-->' loop
  219.     with a few refinements.  'catalog' knows which results to return
  220.     by looking at the REQUEST variable, which contains the input from
  221.     the 'search' form.
  222.  
  223.         o In the source of 'report', find the following line::
  224.  
  225.             <tr><!--#var title--></tr>
  226.         
  227.         o Replace it with this::
  228.  
  229.             <tr>
  230.              <a href="<!--#var "catalog.getpath(data_record_id_)"-->">
  231.               <!--#var title-->
  232.              </a>
  233.             </tr>
  234.  
  235.     This is a little confusing at first.  Keep in mind that ZCatalog
  236.     does not return a list of your database objects.  What it returns
  237.     are actually fairly unintelligent instances of a Record subclass.
  238.     These record objects contain copies of data from attributes of
  239.     catalog objects.  The 'ZCatalog' 'MetaData Table' view defines
  240.     which attributes are copied.
  241.  
  242.     (By default, these record objects are just SLIGHTLY more
  243.     intelligent than a raw tuple.  'Catalog' can be told to use a
  244.     custom, intelligent class for results.  Please see the 'Catalog'
  245.     __init__ method in 'lib/python/Products/ZCatalog/Catalog.py' for
  246.     more information.)
  247.  
  248.     Fortunately, ZCatalog provides a utility function for going from
  249.     result objects to the object's path.  It is called, aptly enough,
  250.     'getpath'.  'getpath' expects to be passed the unique integer
  251.     identifier of the cataloged object.  Results store that id as
  252.     'data_record_id_'.
  253.  
  254.     Commit this change, and perform another search.  Now the title can
  255.     be clicked on to take you to the full page.
  256.     
  257.   Example cataloging custom objects
  258.  
  259.     As if full-text searches of your entire site weren't good enough,
  260.     ZCatalog can also catalog Z Classes, Products, and in fact any
  261.     Python object you can put in a ZODB.  Here is an example using a Z
  262.     Class, but the principles apply to any kind of object.
  263.  
  264.     First, we're going to need something to catalog.  Follow the 'Z
  265.     Class' tutorial to create the CD 'Z Class'.  Back?  Good.
  266.  
  267.         o Create a folder, 'CDs', and create a number of instances of
  268.         the CD Z Class in it.
  269.  
  270.     'cd1' through 'cd5' should be plenty.  Remember to fill each of
  271.     them in from their Properties view.
  272.  
  273.     Now we want to create a searchable catalog of CDs.
  274.  
  275.       o Go to the 'CDs' folder and create a 'ZCatalog' with an ID 'cd_cat'.
  276.  
  277.       o Click on the objects Indexes view.
  278.  
  279.     This screen shows that, by default, 'ZCatalog' is interested in an
  280.     object's 'id','title', 'meta_type', and
  281.     'bobobase_modification_time'.  You will almost always want to
  282.     index additional information.  In this case, we would also like to
  283.     index the artist and description of CDs.
  284.  
  285.       o Type 'artist' into the 'Add Index' field. 
  286.  
  287.     For the sake of example, we're going to use a FieldIndex index for
  288.     artist.  This will give us the option of putting an HTML SELECT
  289.     box for artists on the search form.
  290.  
  291.       o Select FieldIndex from the Index type drop down, and click
  292.       'Add'.
  293.  
  294.       o Also add an index for 'description', but leave TextIndex
  295.       selected.
  296.  
  297.     This will allow us to search for individual words within the
  298.     description.
  299.  
  300.       o Click on 'MetaData Table'.
  301.  
  302.     This is where we tell the 'ZCatalog' what attributes of cataloged
  303.     objects to cache.  These cached values are available from search
  304.     results without having to look up the actual indexed object.  The
  305.     tradeoff for the speed is extra memory, as information from the
  306.     content is duplicated in the 'ZCatalog'.
  307.  
  308.     You will probably want to keep the schema light-weight, so we're
  309.     not going to add 'description' to it.  Type 'artist' in the 'Add
  310.     column' field and click 'Add'.
  311.  
  312.       o Click on the 'Find Items to Catalog' view.
  313.  
  314.     This is the interface you use to tell the 'ZCatalog' which items
  315.     to index.  Right now, beside 'Find objects of type:', 'All types'
  316.     is selected.
  317.  
  318.       o Deselect 'All types'.
  319.  
  320.       O Scroll down and select CD.
  321.  
  322.     You could use the rest of the form to be more specific, but since
  323.     we want to catalog all the CDs,
  324.  
  325.       o Click 'Find'.
  326.  
  327.     'ZCatalog' will report 'Found 5 items.'  It is now giving you an
  328.     opportunity to exclude some of the matched items from the index.
  329.     Again, we want all of them, so,
  330.  
  331.       o Click 'Catalog Items'.
  332.  
  333.     You should at this point see a list of the indexed objects.  Also
  334.     of note is the 'Update Catalog' button.  You have to use it
  335.     whenever you want your 'ZCatalog' to notice changes you've made to
  336.     the objects it's indexed.
  337.  
  338.   Creating Search Forms And Result Reports
  339.  
  340.     This catalog isn't much good without some way of querying it.
  341.  
  342.       o Go back to your 'CDs' folder's management screen and add a Z
  343.         Search Interface.
  344.  
  345.     The search add form will automatically detect your cd_cat
  346.     'ZCatalog' and offer it as a searchable document.  Make sure it is
  347.     selected.
  348.  
  349.       o Fill in 'cd_report' for 'Report ID' and 'cd_search' for
  350.         'Search Input ID'.
  351.  
  352.     Those are the ids of two DTML methods that will be generated in
  353.     the 'CDs' folder.
  354.  
  355.       o Click 'Add'.
  356.  
  357.       o View the 'cd_search' Catalog (at, for example,
  358.         http://localhost:9673/CDs/cd_search).
  359.  
  360.     You will see a basic search interface, with fields for searching
  361.     on 'title', modification date, 'id', 'artist', 'meta type' and
  362.     'description'.  If you fill in one more more of the fields and
  363.     click 'Submit Query', cd_report will be displayed.  It is passed
  364.     the search criteria and uses it to get a list from cd_cat to
  365.     iterate over.  It is merely displaying the information from the
  366.     ZCatalog's MetaData table, but of course it can be enriched.
  367.  
  368.     Try a few more searches.  You'll find that you can type any single
  369.     word from the title or description and get a match, but for artist
  370.     you must type the exact string.  That's because artist was indexed
  371.     as a FieldIndex, which gives us an opportunity to present a more
  372.     convenient interface.
  373.  
  374.     Go back to the 'cd_search' management interface, and change
  375.     it's source to look like this::
  376.  
  377.   <xmp>
  378.   <!--#var standard_html_header-->
  379.   <form action="cd_report" method="get">
  380.   <h2><!--#var document_title--></h2>
  381.   Enter query parameters:<br><table>
  382.   <tr><th>Title</th>
  383.       <td><input name="title"
  384.                  width=30 value=""></td></tr>
  385.   <tr><th>Artist</th>
  386.       <td>
  387.        <select name="artist">
  388.         <option value="">All</option>
  389.         <!--#in expr="cd_cat.uniqueValuesFor('artist')"-->
  390.          <option value="<!--#var sequence-item-->">
  391.           <!--#var sequence-item-->
  392.          </option>
  393.         <!--#/in-->
  394.        </select>
  395.       </td>
  396.   </tr>
  397.   <tr><th>Description</th>
  398.       <td><input name="description"
  399.                  width=30 value=""></td></tr>
  400.   <tr><td colspan=2 align=center>
  401.   <input type="SUBMIT" name="SUBMIT" value="Submit Query">
  402.   </td></tr>
  403.   </table>
  404.   </form>
  405.   <!--#var standard_html_footer-->
  406.   </xmp>
  407.  
  408.     This is a search form somewhat more appropriate for the CD 'Z
  409.     Class'.  Unrelated fields have been removed, and the 'artist'
  410.     field has been changed to a drop-down menu.  Let's augment the
  411.     output of 'cd_report' to make the title a link to the actual CD
  412.     object.
  413.  
  414.     Taking a look at 'cd_report', note that the search results are
  415.     obtained with a simple '<!--#in cd_cat ...-->' tag.  The search
  416.     criteria is automatically obtained by the 'ZCatalog' from the form
  417.     input.  The line we're interested in is this one::
  418.  
  419.             <td><!--#var title--></td>
  420.  
  421.     Change it to read::
  422.  
  423.             <td>
  424.              <a href="<!--#var "cd_cat.getpath(data_record_id_)"-->">
  425.               <!--#var title-->
  426.              </a>
  427.             </td>
  428.  
  429.     Now, assuming you have added the index_html document template to
  430.     your CD 'Z Class', clicking on a search result will take you to
  431.     the CD's detailed display.
  432.  
  433.   Using 'ZCatalog' In A Zope Site
  434.  
  435.     The 'ZCatalog' provides high-speed access to what is on your site.
  436.     Thus, the 'ZCatalog' can be used to re-engineer the way your site
  437.     is laid out.
  438.  
  439.     For instance, a Slashdot-style presentation is simple.  Just
  440.     insert some DTML that asks the 'ZCatalog' for recent items.
  441.     Alternatively, a Site Map is nothing more than presenting the
  442.     contents of the catalog.  A page with tree-based browsing of
  443.     software packages by category is also easy.  Perhaps you'd like to
  444.     provide a link that lists all the packages the current user has
  445.     authored.
  446.  
  447.     Thus, the 'ZCatalog' isn't just about searching.  It can be used
  448.     as the DTML-scriptable engine for browsing a site as well.
  449.  
  450.     Since the 'ZCatalog' is a normal Zope folderish object, you can
  451.     also create DTML Methods inside it to present the catalog
  452.     contents.  For instance, perhaps you'd like to dump the contents
  453.     of the site as an RDF stream, or do content syndication with RSS.
  454.     These are just DTML Methods that change the 'Content-Type:' and
  455.     send back XML.  All without actually waking up any of the content
  456.     objects in the site.
  457.  
  458.   ZCatalog vs. Catalog
  459.  
  460.     The real star of this package is the 'Catalog' module.  All the
  461.     heavy lifting is done by 'Catalog'.  'ZCatalog' is basically a
  462.     Zope-aware wrapper around Catalog, which can be used on it's own
  463.     outside the Zope framework.  The only requirement is that you are
  464.     using ZODB as your object store.
  465.